package com.tengju.bff.interfaces.login;

import com.tengju.bff.interfaces.login.facade.UserAccountFacade;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.user.application.login.UserAccountService;
import com.tengju.user.application.login.dto.LoginDTO;
import com.tengju.user.application.login.dto.LogoutDTO;
import com.tengju.user.application.login.dto.PwdChangeDTO;
import com.tengju.user.application.login.dto.WeChatLoginDTO;
import com.tengju.user.application.login.vo.LoginVO;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author wuziyang
 */
@Validated
@RestController
@Api(tags = "登陆服务")
@RequestMapping(value = "login/")
@Slf4j
public class UserAccountController {

    @Autowired
    UserAccountService userAccountService;

    @Autowired
    UserAccountFacade userAccountFacade;


    @NotTokenValid()
    @ApiOperation(value = "登陆/注册", notes = "登陆/注册")
    @RequestMapping(value = "login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<LoginVO> login(@Valid @RequestBody LoginDTO loginDTO) throws Exception {

        try {
            return ApiResponse.newSuccess(userAccountFacade.login(loginDTO));
        } catch (DomainException | ApplicationException e) {
            log.warn("手机号:{},登陆失败：",loginDTO.getUsername(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    @RequestMapping(value = "logout", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> logout(@RequestBody LogoutDTO logoutDTO) throws Exception {

        try {

            Boolean result = userAccountFacade.logout(logoutDTO);
            return ApiResponse.newSuccess(result);
        } catch (DomainException | ApplicationException e) {
            log.warn("退出登陆失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "密码变更", notes = "密码变更")
    @RequestMapping(value = "password/change", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<LoginVO> passwordChange(@RequestBody PwdChangeDTO pwdChangeDTO) throws Exception {

        try {
            return ApiResponse.newSuccess(userAccountFacade.pwdChange(pwdChangeDTO));
        } catch (DomainException | ApplicationException | InterfaceException e) {
            log.error("手机号:{},密码变更失败：",pwdChangeDTO.getMobile(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "校验手机号", notes = "校验手机号")
    @RequestMapping(value = "check/mobile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> checkMobile(@RequestParam String areaCode,@RequestParam String mobile) {

        try {
            return ApiResponse.newSuccess(userAccountService.checkMobile(areaCode,mobile));
        } catch (DomainException | ApplicationException e) {
            log.error("手机号:{},校验手机号失败：",mobile, e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }
    }


    @ApiOperation(value = "获取区号字典", notes = "获取区号字典")
    @RequestMapping(value = "area/code", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<String> getAreaCode() {
        try {
            return ApiResponse.newSuccess(userAccountService.getAreaCode());
        } catch (DomainException | ApplicationException e) {
            log.error("获取区号字典失败：", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

    @ApiOperation(value = "微信小程序登陆", notes = "微信小程序登陆")
    @RequestMapping(value = "qyMiniProgram/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @NotTokenValid
    public ApiResponse<LoginVO> wechatLogin(@Valid @RequestBody WeChatLoginDTO weChatLoginDTO) {
        try {
            return ApiResponse.newSuccess(userAccountFacade.weChatLogin(weChatLoginDTO));
        } catch (DomainException | ApplicationException e) {
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        }

    }

}
